package mashuaiproject.mashuaiproject.serivce;

import mashuaiproject.mashuaiproject.enums.CommentTypeEnum;
import mashuaiproject.mashuaiproject.exption.CustomizeCode;
import mashuaiproject.mashuaiproject.exption.CustomizeException;
import mashuaiproject.mashuaiproject.mapper.CommentMapper;
import mashuaiproject.mashuaiproject.mapper.CommentSonMapper;
import mashuaiproject.mashuaiproject.mapper.NotificationMapper;
import mashuaiproject.mashuaiproject.mapper.QuesionMapper;
import mashuaiproject.mashuaiproject.model.CommentSon;
import mashuaiproject.mashuaiproject.model.Comments;
import mashuaiproject.mashuaiproject.model.Notification;
import mashuaiproject.mashuaiproject.model.Quesions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentSerivce {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private QuesionMapper quesionMapper;
    @Autowired
    private CommentSonMapper commentSonMapper;
    @Autowired
    private NotificationMapper notificationMapper;


    public void insertComment(Comments comments) {
        System.err.println("用户回复的评论："+comments.toString());
        if(comments.getParent_id()==null || comments.getParent_id()==0){
            throw new CustomizeException(CustomizeCode.TARGET_NOT_FOUND);
        }
        if(comments.getType()==null || !CommentTypeEnum.isExist(comments.getType())){
            throw new CustomizeException(CustomizeCode.TYPE_PARAM_WORME);
        }
        if(comments.getType()==CommentTypeEnum.COMMENT.getType()){
            //回复评论
            System.out.println("进入回复评论的条件");
            Comments comments1=commentMapper.selectByPrimaryKey(comments.getParent_id());
            if(comments1==null){
                throw new CustomizeException(CustomizeCode.COMENT_NOT_FOUNF);
            }
            commentMapper.insertComment(comments);//插入评论
        }else{

            System.out.println("进入回复问题--的条件");
            Quesions quesions=quesionMapper.selectByPrimarytKey(comments.getParent_id());
            if(quesions==null){
                throw new CustomizeException(CustomizeCode.COMENT_NOT_FOUNF);
            }
            commentMapper.insertComment(comments);
            //写上评论数
             quesionMapper.updateCommentount(comments.getParent_id());
            //回复问题（点击回复文章）
             //回复文章的问题
            //创建回复通知对象
            //根据文章的id查找对应的用户id，被回复的人
            int receriverId=commentMapper.getReceiverd2(comments.getParent_id());
            Notification notification=new Notification();
            notification.setGmt_create(System.currentTimeMillis());//回复的时间
            notification.setNotifier(comments.getCommentator());//回复用户的id
            notification.setReceiver(receriverId);//被回复人的id
            notification.setType(1);//代表回复的是文章
            notification.setStatus(1);//默认是1代表未读。2代表已读
            notification.setQuesion_id(comments.getParent_id());//文章的id
            notification.setContent(comments.getContent());
            //查找到
            notificationMapper.insert(notification);
        }
    }

//插入子评论
    public int insertCommentSon(CommentSon commentSon) {
        System.out.println("回复父评论："+commentSon.toString());
        int insertCommentSon=commentSonMapper.inserSonComment(commentSon);
        //根据父评论的id，。找到对应的user id
        Integer receiverId=commentMapper.getReceiverd(commentSon.getFather_id());
        System.out.println(commentSon.getUser_id()+"回复"+receiverId);


        //创建回复通知对象
        Notification notification=new Notification();
        notification.setGmt_create(System.currentTimeMillis());//回复的时间
        notification.setNotifier(commentSon.getUser_id());//回复用户的id
        notification.setReceiver(receiverId);//被回复人的id
        notification.setType(2);//代表回复的是评论
        notification.setStatus(1);//默认是1代表未读。2代表已读
        notification.setQuesion_id(commentSon.getQuesion_id());//文章的id
        notification.setContent(commentSon.getContent());
        Integer comment_id=commentMapper.sleectById(commentSon.getFather_id());
        notification.setComment_id(comment_id);
        //将数据插入·数据库
        notificationMapper.insert(notification);


        return insertCommentSon;
    }

    public int updateCommentSonCount(Integer father_id) {
        int updateCommentSonCounts=commentMapper.updateCommetSonCount(father_id);
        int commentSonCount=commentMapper.selectCommetSonCount(father_id);
        return commentSonCount;
    }
}
